package com.aaa.sbm.controller;

import com.aaa.sbm.entity.Dept;
import com.aaa.sbm.service.DeptService;
import com.aaa.sbm.util.Page;
import com.aaa.sbm.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("dept") // 请求路径
public class DeptController {

    //依赖注入
    @Resource
    private DeptService deptService;




    @PostMapping("add")  //新建/添加资源
    public Result  add(@RequestBody Dept dept){
        int i = deptService.add(dept);
        if(i>0){
            return new Result(200,  "添加成功",i);
        }else {
            return new Result(500,  "添加失败",i);
        }
    }



    @PostMapping("update")  //更新资源
    public Result  update( @RequestBody Dept dept){
        int i = deptService.update(dept);
        if(i>0){
            return new Result(200,  "更新成功",i);
        }else {
            return new Result(500,  "更新失败",i);
        }
    }


    @DeleteMapping("deleteById")  //更新资源
    public Result  deleteById(Integer deptNo){
        int i = deptService.deleteById(deptNo);
        if(i>0){
            return new Result(200,  "删除成功",i);
        }else {
            return new Result(500,  "删除失败",i);
        }
    }


    @PostMapping("queryPage")

    public Result  queryPage(@RequestBody Page<Dept> page){
        try {
            PageInfo pageInfo = deptService.queryPage(page);
            return new Result(200,  "查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(500,  "查询失败",null);
    }

}
